package com.ymatou.iapi.csim.testcase;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.csim.parameter.CheckScoreMessageStatusBean;
import com.ymatou.iapi.csim.parameter.SendMessageCSBean;
import com.ymatou.iapi.csim.service.CheckScoreMessageStatusCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CsimCallService;
import com.ymttest.database.model.ymt_cssession;
import com.ymttest.database.sqlwapper.CSIM.ymt_cssessionWapper;
import com.ymttest.database.sqlwapper.CSIM.ymt_onlinecsWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
/**
*校验微信用户是否可以评分
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_CheckScoreMessageStatus{
	private static CheckScoreMessageStatusBean bean;
	private static CheckScoreMessageStatusCall call;
	private CsimCallService service=new CsimCallService();
	private Integer csId=Integer.parseInt(EnvSetup.getData("csId").toString());
	private static Integer userId=Integer.parseInt(EnvSetup.getData("userId_IM").toString());
	private static ymt_cssessionWapper csSessionWapper=new ymt_cssessionWapper();
	private static ymt_onlinecsWapper onlinecsWapper=new ymt_onlinecsWapper();
	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("校验微信用户是否可以评分");
	}

	@Before
	public void caseUp() {
		bean=new CheckScoreMessageStatusBean();
		call=new CheckScoreMessageStatusCall();
		onlinecsWapper.deleteBy(MapUtil.hashMap("CSId",csId));
		csSessionWapper.deleteByUserId(MapUtil.hashMap("UserId",userId));
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CheckScoreMessageStatus_001() {
		Logger.start(true,"校验微信用户是否可以评分-可以评分");
		try {
 
			//客服上线
			service.getOnlineCS(csId);
			//打开会话
			ymt_cssession cssession=service.getOpenSession(csId,userId);
			//发消息
			SendMessageCSBean sendMessageCSBean=service.setSendMessageCSBean();
			sendMessageCSBean.setSessionId(cssession.getId());
			sendMessageCSBean.setCsId(csId);
			sendMessageCSBean.setUserId(userId);
			service.setSendMessageCSCall(sendMessageCSBean);
			
			ymt_cssession session=csSessionWapper.selectByPrimaryKey(cssession.getId());
			session.setSourceParams("微信");
			session.setMessageSource(2);
			session.setIsSendScore(1);
			csSessionWapper.updateByPrimaryKey(session);
			
			bean.setSessionId(cssession.getId());
			call.setData(bean);
			call.callService();		
			
			Logger.verifyEquals("200",call.getStatus(), "验证status");
			Logger.verifyEquals("",call.getMsg(), "验证Msg");
			Logger.verifyEquals(true, call.getResult().getBoolean("Success"), "验证Success");
			Logger.verifyEquals(0, call.getResult().getInt("Result"), "验证Result");
			Logger.verifyEquals(0, call.getResult().getInt("Score"), "验证Score");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CheckScoreMessageStatus_002() {
		Logger.start(true,"校验微信用户是否可以评分-验证没有发送评分，不可以评分");
		try {
			//客服上线
			service.getOnlineCS(csId);
			//打开会话
			ymt_cssession cssession=service.getOpenSession(csId,userId);
			//发消息
			SendMessageCSBean sendMessageCSBean=service.setSendMessageCSBean();
			sendMessageCSBean.setSessionId(cssession.getId());
			sendMessageCSBean.setCsId(csId);
			sendMessageCSBean.setUserId(userId);
			service.setSendMessageCSCall(sendMessageCSBean);
			
			bean.setSessionId(cssession.getId());
			call.setData(bean);
			call.callService();		
			
			Logger.verifyEquals("200",call.getStatus(), "验证status");
			Logger.verifyEquals("",call.getMsg(), "验证Msg");
			Logger.verifyEquals(true, call.getResult().getBoolean("Success"), "验证Success");
			Logger.verifyEquals(-1, call.getResult().getInt("Result"), "验证Result");
			Logger.verifyEquals(0, call.getResult().getInt("Score"), "验证Score");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CheckScoreMessageStatus_003() {
		Logger.start(true,"校验微信用户是否可以评分-验证session不存在，不可以评分");
		try {
			
			bean.setSessionId("111111");
			call.setData(bean);
			call.callService();		
			
			Logger.verifyEquals("200",call.getStatus(), "验证status");
			Logger.verifyEquals("",call.getMsg(), "验证Msg");
			Logger.verifyEquals(true, call.getResult().getBoolean("Success"), "验证Success");
			Logger.verifyEquals(-1, call.getResult().getInt("Result"), "验证Result");
			Logger.verifyEquals(0, call.getResult().getInt("Score"), "验证Score");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CheckScoreMessageStatus_004() {
		Logger.start(true,"校验微信用户是否可以评分-验证已评分");
		try {
			//客服上线
			service.getOnlineCS(csId);
			//打开会话
			ymt_cssession cssession=service.getOpenSession(csId,userId);
			//发消息
			SendMessageCSBean sendMessageCSBean=service.setSendMessageCSBean();
			sendMessageCSBean.setSessionId(cssession.getId());
			sendMessageCSBean.setCsId(csId);
			sendMessageCSBean.setUserId(userId);
			service.setSendMessageCSCall(sendMessageCSBean);
			
			ymt_cssession session=csSessionWapper.selectByPrimaryKey(cssession.getId());
			session.setSourceParams("微信");
			session.setMessageSource(2);
			session.setIsSendScore(1);
			session.setScore(5);
			csSessionWapper.updateByPrimaryKey(session);
			
			bean.setSessionId(cssession.getId());
			call.setData(bean);
			call.callService();		
			
			Logger.verifyEquals("200",call.getStatus(), "验证status");
			Logger.verifyEquals("",call.getMsg(), "验证Msg");
			Logger.verifyEquals(true, call.getResult().getBoolean("Success"), "验证Success");
			Logger.verifyEquals(1, call.getResult().getInt("Result"), "验证Result");
			Logger.verifyEquals(true, call.getResult().getInt("Score")>0, "验证Score");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CheckScoreMessageStatus_005() {
		Logger.start(true,"校验微信用户是否可以评分-验证失效，不可评分");
		try {
			//客服上线
			service.getOnlineCS(csId);
			//打开会话
			ymt_cssession cssession=service.getOpenSession(csId,userId);
			//发消息
			SendMessageCSBean sendMessageCSBean=service.setSendMessageCSBean();
			sendMessageCSBean.setSessionId(cssession.getId());
			sendMessageCSBean.setCsId(csId);
			sendMessageCSBean.setUserId(userId);
			service.setSendMessageCSCall(sendMessageCSBean);
			
			ymt_cssession session=csSessionWapper.selectByPrimaryKey(cssession.getId());
//			session.setSourceParams("微信");
//			session.setMessageSource(2);
			session.setIsSendScore(1);

          //  SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date=new Date();
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(Calendar.DATE, -3);
			session.setLastMessageTime(calendar.getTime());
			csSessionWapper.updateByPrimaryKey(session);
			
			bean.setSessionId(cssession.getId());
			call.setData(bean);
			call.callService();		
			
			Logger.verifyEquals("200",call.getStatus(), "验证status");
			Logger.verifyEquals("",call.getMsg(), "验证Msg");
			Logger.verifyEquals(true, call.getResult().getBoolean("Success"), "验证Success");
			Logger.verifyEquals(2, call.getResult().getInt("Result"), "验证Result");
			Logger.verifyEquals(0, call.getResult().getInt("Score"), "验证Score");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}